package com.tim.app.ui.dialog;

import android.animation.ObjectAnimator;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Transformation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.application.library.net.StringResponseCallback;
import com.tim.app.R;
import com.tim.app.manager.WindowUtils;
import com.tim.app.server.api.API_User;
import com.tim.app.server.logic.UserManager;
import com.tim.app.ui.activity.person_center.InputInvateCodeActivity;
import com.tim.app.ui.activity.setting.RegisterActivity;
import com.tim.app.util.AnimUtils;
import com.tim.app.util.AppDate;
import com.tim.app.util.MyYAnimation;
import com.tim.app.util.TextUtil;

/**
 * Created by wdy on 2017/8/18.
 */

public class RedBagDialog extends DialogFragment {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        if (getArguments() != null) {
//            big = (String) getArguments().get("big");
//        }
        money = getArguments().getString("money");
        id = getArguments().getString("id");
        position = getArguments().getInt("position");
        type = getArguments().getString("type");
        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    }

    private String money;
    private String id;
    private int position;
    private String type;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getActivity(), R.style.DialogStyle);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(false);
        View view = View.inflate(getActivity(), R.layout.dialog_redbag, null);
        dialog.setContentView(view);
        initView(view);
        return dialog;
    }

    private ImageView iv_redbag_open;
    private MyYAnimation myYAnimation;
    private ImageView iv_redbag_top, iv_redbag_close, iv_redbag_bottom;
    private TextView tv_receive_redbag, tv_congratulations, tv_save_money, tv_wx_get, tv_congratulations_again, tv_money_monent, tv_newsuer;
    private TextView btn_commit;

    private void initView(View view) {
        btn_commit = (TextView) view.findViewById(R.id.btn_commit);
        tv_save_money = (TextView) view.findViewById(R.id.tv_save_money);
        tv_newsuer = (TextView) view.findViewById(R.id.tv_newsuer);
        tv_money_monent = (TextView) view.findViewById(R.id.tv_money_monent);
        tv_congratulations_again = (TextView) view.findViewById(R.id.tv_congratulations_again);
        tv_wx_get = (TextView) view.findViewById(R.id.tv_wx_get);
        tv_congratulations = (TextView) view.findViewById(R.id.tv_congratulations);
        tv_receive_redbag = (TextView) view.findViewById(R.id.tv_receive_redbag);
        iv_redbag_open = (ImageView) view.findViewById(R.id.iv_redbag_open);
        iv_redbag_top = (ImageView) view.findViewById(R.id.iv_redbag_top);
        iv_redbag_close = (ImageView) view.findViewById(R.id.iv_redbag_close);
        iv_redbag_bottom = (ImageView) view.findViewById(R.id.iv_redbag_bottom);
        tv_money_monent.setText("￥" + money);
        iv_redbag_open.setOnClickListener(onClickListener);
        iv_redbag_close.setOnClickListener(onClickListener);
        btn_commit.setOnClickListener(onClickListener);
        myYAnimation = new MyYAnimation();
        myYAnimation.setRepeatCount(1); //旋转的次数（无数次）Animation.INFINITE
        myYAnimation.setAnimationListener(animationListener);
        tv_newsuer.setText(AppDate.redBag.get(type));
        if ("sign".equals(type)) {
            btn_commit.setText("确定");
        } else if ("register".equals(type)) {
            if (UserManager.ins().isLogin() && UserManager.ins().getLoginUser() != null && TextUtils.isEmpty(UserManager.ins().getLoginUser().getInviteUuid())) {
                btn_commit.setText("输入邀请码再得1元");
            } else {
                btn_commit.setText("确定");
            }
        } else if ("invitation".equals(type)) {
            btn_commit.setText("确定");
        } else if ("be_invitation".equals(type)) {
            btn_commit.setText("确定");
        }


    }

    private Animation.AnimationListener animationListener = new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
//            ObjectAnimator animator = ObjectAnimator.ofFloat(iv_redbag_top, "translationY", 360f, 0f);
//            animator.setDuration(5000);
//            animator.start();
            tv_congratulations.setVisibility(View.GONE);
            tv_wx_get.setVisibility(View.GONE);
            tv_newsuer.setVisibility(View.GONE);
            tv_congratulations_again.setVisibility(View.VISIBLE);
            //顶部背景
            Animation animation1 = AnimationUtils.loadAnimation(getContext(), R.anim.red_top_out);
            animation1.setFillAfter(true);
            iv_redbag_top.setAnimation(animation1);
            animation1.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    iv_redbag_open.setBackgroundResource(R.drawable.iv_money);
                    isOpen = true;
                    //          tv_congratulations_again.setVisibility(View.VISIBLE);
//                    tv_save_money.setVisibility(View.VISIBLE);
//                    tv_money_monent.setVisibility(View.VISIBLE);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
            //开
            Animation openAni = AnimationUtils.loadAnimation(getContext(), R.anim.open_top_out);
            openAni.setFillAfter(true);
            iv_redbag_open.setAnimation(openAni);
            //文字
            Animation textAni = AnimationUtils.loadAnimation(getContext(), R.anim.text_top_out);
            textAni.setFillAfter(true);
            tv_receive_redbag.setAnimation(textAni);
            //底部背景
            Animation bottomAni = AnimationUtils.loadAnimation(getContext(), R.anim.red_bottom_out);
            bottomAni.setFillAfter(true);
            iv_redbag_bottom.setAnimation(bottomAni);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    };
    private boolean isOpen;
    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == iv_redbag_open) {
                if (UserManager.ins().isLogin()) {
                    openRedbag();
                    iv_redbag_open.startAnimation(myYAnimation);
                } else {
                    dismiss();
                    //  WindowUtils.getInStance().clearList();
                    Intent intent = new Intent(getContext(), RegisterActivity.class);
                    intent.putExtra("login", true);
                    startActivity(intent);
                }


            } else if (v == iv_redbag_close) {
                dismiss();
                if (isOpen) {
                    if (onDimissListener != null) {
                        onDimissListener.onOpenFinish(position);
                    }
                }
            } else if (v == btn_commit) {
                dismiss();
                if (onDimissListener != null) {
                    onDimissListener.onOpenFinish(position);
                }
                if ("输入邀请码再得1元".equals(btn_commit.getText().toString().toLowerCase())) {
                    startActivity(new Intent(getContext(), InputInvateCodeActivity.class));
                }
            }
        }
    };

    private void openRedbag() {
        API_User.ins().openRedbag("", UserManager.ins().getUserId(), id, new StringResponseCallback() {
            @Override
            public boolean onStringResponse(String resultValue, int errCode, String errMsg, int id, boolean formCache) {
                return false;
            }
        });
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        if (isOpen) {
            if (onDimissListener != null) {
                onDimissListener.onOpenFinish(position);
            }
        }
        if (onDimissListener != null) {
            onDimissListener.onDismiss(dialog);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (onDimissListener != null) {
            onDimissListener.onShow();
        }
    }

    public void setOnDimissListener(OnDimissListener onDimissListener) {
        this.onDimissListener = onDimissListener;
    }

    private OnDimissListener onDimissListener;

    public interface OnDimissListener {
        void onDismiss(DialogInterface dialog);

        void onShow();

        void onOpenFinish(int id);
    }
}
